package com.bridge.jdbc;

import com.bridge.NetDiskUrl;

import java.io.InputStream;
import java.sql.*;
import java.util.List;
import java.util.Properties;

/**
 * 封装数据库连接和关闭
 */

public class JDBCUtils {
    public static Connection getConnection() throws Exception {
        //读取配置文件基本信息
        InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
        Properties pros = new Properties();
        pros.load(is);
        String user = pros.getProperty("user");
        String password = pros.getProperty("password");
        String url = pros.getProperty("url");
        String driverClass = pros.getProperty("driverClass");
        //加载驱动
        Class.forName(driverClass);
        //获取连接
        return DriverManager.getConnection(url, user, password);
    }

    public static void closeResource(Connection conn, Statement ps) {
        try {
            if (ps != null)
                ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (conn != null)
                conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void closeResource(Connection conn, Statement ps, ResultSet rs) {
        try {
            if (ps != null)
                ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (conn != null)
                conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (rs != null)
                rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void insert() throws Exception {
        Connection conn = JDBCUtils.getConnection();
        Statement st = conn.createStatement();
        for (int i = 1; i <= 20000; i++) {
            String sql = "insert into goods(name) values('name_" + i + "')";
            st.execute(sql);
        }
    }

    public static void testInsert1(){
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            long start = System.currentTimeMillis();//计算花费时间
            conn = JDBCUtils.getConnection();
            String sql = "insert into goods(name) values(?)";
            ps = conn.prepareStatement(sql);
            for(int i = 1;i<=20000;i++){
                ps.setObject(1,"name_"+i);
                ps.execute();
            }
            long end = System.currentTimeMillis();
            System.out.println(end-start+"s");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(conn,ps);
        }
    }


    /**
     * 批量提交
     */
    public static void batchCommitInsert(List<NetDiskUrl> list) {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            long start = System.currentTimeMillis();//计算花费时间
            conn = JDBCUtils.getConnection();
            conn.setAutoCommit(false);//不允许自动提交数据
            String sql = "INSERT INTO test.t_baidu_url (id,title, sub_title, url, code) VALUES(? , ?, ?, ?, ?);";
            ps = conn.prepareStatement(sql);
            for (int i= 0;i < list.size();i++) {
                NetDiskUrl diskUrl = list.get(i);
                ps.setObject(1, i);
                ps.setObject(2, diskUrl.getTitle());
                ps.setObject(3, diskUrl.getSubTitle());
                ps.setObject(4, diskUrl.getUrl());
                ps.setObject(5, diskUrl.getCode());
                //1、“攒”sql
                ps.addBatch();
                if (i % 500 == 0) {
                    //2、执行batch
                    ps.executeBatch();
                    //3、清空batch
                    ps.clearBatch();
                }
            }
            //提交数据
            conn.commit();
            long end = System.currentTimeMillis();
            System.out.println(end - start + "ms");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, ps);
        }
    }
}
